//index.js
//获取应用实例
const app = getApp()
const questionService = require('../../service/question.js');
const commonService = require('../../service/common.js');
const homeService = require('../../service/home.js');
const mineService = require('../../service/mine.js');

Page({
  data: {
    title:"问答详情",
    danmuList:[],
    id: "",
    queryParams: {
      page: 1,
      num: 10
    },
    isLast: false,
    list:[],
    detailInfo: {},
    checkLikecolor: 'D8D7D7',
    checkLikeName: '点赞',
    checkLikebindtap: 'checklike',
    collectcolor: 'D8D7D7',
    collectname: '收藏',
    collectbindtap:'collect'
  },
  onLoad: function (options) {
    let id = options.id;
    this.setData({
      id: id
    })
    let d = {
      wendaId: id
    }
    this.getContentList(d);
    this.getCommentList();
  },
  getContentList(d) {
    questionService.getQuestionDetail(d).then(res => {
      if (res.resCode == 0) {
        this.setData({
          detailInfo: res.object
        });
      }
    })
  },
  getCommentList() {
    let d = {
      objectId: this.data.id,
      ...this.data.queryParams
    };
    commonService.getCommentList(d).then(res => {
      if (res.resCode == 0) {
        if (!res.object.length) {
          this.setData({
            isLast: true
          });
          return false;
        }
        
        this.setData({
          list: [
            ...this.data.list,
            ...res.object
          ]
        });
      }
    });
  },
  onReachBottom() {
    if (this.data.isLast) return false;
    this.setData({
      queryParams: {
        page: ++this.data.queryParams.page,
        num: 10
      }
    })
    this.getCommentList();
  },
  sendComment(e){
    let obj = e.detail;
    let d = {
      objectId: this.data.id,
      type:0,
      unionId: app.globalData.userinfo.unionId,
      ...obj
    }
    console.log(d);
    commonService.addComment(d).then(res => {
      if(res.resCode == 0){
        this.data.list.unshift(res.object);
        this.setData({
          list:this.data.list
        });
        wx.showToast({
          title: '评论成功',
          icon:"none"
        });
      }
    })
  },
  collect(){
    if(!app.checkLogin()) return false;
    let d = {
      objectId: this.data.id,
      objectType:0,
      unionId:app.globalData.userinfo.unionId
    };
    console.log(d);
    mineService.collect(d).then(res => {
      if(res.resCode == 0){
        wx.showToast({
          title: '收藏成功',
          icon:"none"
        })
        this.setData({
          collectcolor:'CD0000',
          collectname:'已收藏',
          collectbindtap:''
        });
      }
    })
  },
  checklike(){
    if (!app.checkLogin()) return false;
    let d = {
      wendaId: this.data.id,
      objectType:0,
      unionId:app.globalData.userinfo.unionId
    };
    console.log(d);
    homeService.checkLikeWenda(d).then(res => {
      if(res.resCode == 0){
        wx.showToast({
          title: '点赞成功',
          icon:"none"
        })
        this.setData({
         checkLikecolor:'CD0000',
         checkLikeName:'已赞',
         checkLikebindtap:''
        });
      }
    })
  }
})
